# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import time

import requests
from bs4 import BeautifulSoup


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.


headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,'
              'application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Cookie': 'SECKEY_ABVK=aX8pAsKDLs1YifKiRP9oUzPs0ajK2TEhFETOt9dDu2g%3D; '
              'BMAP_SECKEY=2ULR5SJJbFSxePybZpIH'
              '-v47eXJ_T0W1GIngEQqLr8qQLh0WFZaqTY1nBVDQIa_jgoErh80kGKnHNyRLXgvWciNaSGkjJeulFvb_349i4kNqkwEpHK6tlKv'
              '-vIjcz9dh0SvDJwFwGucMvEDvo9qURLwfuC0ufMDr2KdYkz8YnvY_XjB8khy0YCDa84sDXVdt; '
              'lianjia_uuid=d81a6c3d-29d1-474d-81da-435d3bf0c888; '
              'crosSdkDT2019DeviceId=-hpwhci-ccfarq-v8u56gllftt669d-tamjvmt2l; select_city=110000; '
              'Hm_lvt_9152f8221cb6243a53c83b956842be8a=1681453507,1683523551,1683621305; '
              'lianjia_ssid=7794c4cb-1484-45a4-9984-894c8e2a6fd7; '
              'sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221821599dc8add2-08f2998b7341ab-1d525635-3686400'
              '-1821599dc8b5ab%22%2C%22%24device_id%22%3A%221821599dc8add2-08f2998b7341ab-1d525635-3686400'
              '-1821599dc8b5ab%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6'
              '%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.google.com%2F'
              '%22%2C%22%24latest_referrer_host%22%3A%22www.google.com%22%2C%22%24latest_search_keyword%22%3A%22%E6'
              '%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%7D%7D; '
              'hy_data_2020_id=187ffa6c939e61-0011ee6ceeac2c-1c525634-3686400-187ffa6c93aff9; '
              'hy_data_2020_js_sdk=%7B%22distinct_id%22%3A%22187ffa6c939e61-0011ee6ceeac2c-1c525634-3686400'
              '-187ffa6c93aff9%22%2C%22site_id%22%3A341%2C%22user_company%22%3A236%2C%22props%22%3A%7B%7D%2C'
              '%22device_id%22%3A%22187ffa6c939e61-0011ee6ceeac2c-1c525634-3686400-187ffa6c93aff9%22%7D; '
              'sajssdk_2020_cross_new_user=1; Hm_lpvt_9152f8221cb6243a53c83b956842be8a=1683621410; '
              'srcid=eyJ0Ijoie1wiZGF0YVwiOlwiNGRmYzRlNzRkZGVkNjQ5Yzk4MTBmNzNlMjZhMzM3MWFmODM2ZTBmZWExNTk3OThjZGYyMGU1ZTJmMTU2MmJlYmY1NzIxMDFjZjk3OGMwMWIwZTQwMWVlOWMzYWVmNWJiOWFiZDIwMWI5YWUyOGM0ZDM1ODU4YTZjNzc1ZjBjNDY0ZWViMmJjMTdhZmM4MjA0MWRiZThhYTZiNWU1ZmMyODMyMWQwYTAyYWY4NzNmMDYwYTMxNzg1YTMxNjczODE2MWQ5OWUzMWMwMDUyNjUzODE5YmFjMTNkYzFmYzI1OTU1ZGJiMWQ3NjMwMjA4ZTZmYTRhMjNlOGUzMTRhNjdhY1wiLFwia2V5X2lkXCI6XCIxXCIsXCJzaWduXCI6XCJmZDRlNGQ3MFwifSIsInIiOiJodHRwczovL2JqLmtlLmNvbS9lcnNob3VmYW5nL2NoYW5ncGluZy8iLCJvcyI6IndlYiIsInYiOiIwLjEifQ==',
    'Referer': 'https://bj.ke.com/ershoufang/changping/',
    'Sec-Fetch-Dest': 'document',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/98.0.4758.82 Safari/537.36 '
}

def todayHosuse(url):
    result = requests.get(url, headers=headers).text
    # print(result)
    bf = BeautifulSoup(result, features="html.parser")
    return bf.find(attrs={'class': 'total fl'}).find(name='span').text.strip()

if __name__ == '__main__':
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    total = todayHosuse("https://bj.ke.com/ershoufang/changping/sf1/")
    p200_430 = todayHosuse("https://bj.ke.com/ershoufang/changping/sf1bp200ep430/")
    dt_p200_430 = todayHosuse("https://bj.ke.com/ershoufang/changping/ie2sf1bp200ep430/")
    with open('./cangping.txt','a+') as f:
        f.write(f'{now}:{total},{p200_430},{dt_p200_430}\n')
    print(f'{now},{total},{p200_430},{dt_p200_430}\n')


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
